"""
数据库配置
"""
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from config.settings import settings

# 创建异步数据库引擎
engine = create_async_engine(
    settings.database_url,
    echo=settings.debug,
    pool_pre_ping=True,
    pool_recycle=300
)

# 创建异步会话工厂
SessionLocal = async_sessionmaker(
    autocommit=False, 
    autoflush=False, 
    bind=engine,
    class_=AsyncSession
)

# 创建基础模型类
Base = declarative_base()


async def get_db():
    """获取异步数据库会话"""
    async with SessionLocal() as db:
        try:
            yield db
        finally:
            await db.close()
